package com.example.tree.controller;

import com.example.common.domain.PageResult;
import com.example.common.domain.Result;
import com.example.tree.domain.dto.QueryIssueDTO;
import com.example.tree.domain.dto.TreeIssueDTO;
import com.example.tree.domain.dto.UpdateIssueDTO;
import com.example.tree.domain.vo.IssueReportsVO;
import com.example.tree.service.TreeIssueService;
import com.google.zxing.WriterException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@Tag(name = "古树异常相关")
@Slf4j
@RestController
@RequestMapping("/tree/issue")
@RequiredArgsConstructor
public class TreeIssueController {
    private final TreeIssueService treeIssueService;

    @Operation(summary = "添加古树异常信息")
    @PostMapping
    public Result addTreeIssueInfo(@RequestBody @Validated TreeIssueDTO treeIssueDTO) {
        treeIssueService.addTreeIssueInfo(treeIssueDTO);
        return Result.success();
    }

    /**
     * 获取古树异常信息
     *
     * @param pageNum   页码
     * @param pageSize  每页数量
     * @param treeId    古树id
     * @param issueType 异常类型
     * @param status    状态
     * @return
     */
    @Operation(summary = "获取古树异常信息")
    @GetMapping
    public Result<PageResult<IssueReportsVO>> getTreeIssueInfo(Integer pageNum,
                                                               Integer pageSize,
                                                               Integer treeId,
                                                               Integer issueType,
                                                               Integer status) {
        QueryIssueDTO queryIssueDTO = new QueryIssueDTO(pageSize, pageNum, treeId, issueType, status);
        log.info("获取古树异常信息:{}", queryIssueDTO);
        PageResult<IssueReportsVO> pageResult = treeIssueService.getTreeIssueInfo(queryIssueDTO);
        return Result.success(pageResult);
    }

    @Operation(summary = "删除古树异常信息")
    @DeleteMapping
    public Result deleteTreeIssueInfo(@RequestBody Integer[] ids) {
        log.info("删除古树异常信息:{}", ids);
        treeIssueService.deleteTreeIssueInfo(ids);
        return Result.success();
    }

    @Operation(summary = "修改古树异常信息")
    @PutMapping
    public Result updateTreeIssueInfo(@RequestBody @Validated UpdateIssueDTO updateIssueDTO) {
        log.info("修改古树异常信息:{}", updateIssueDTO);
        treeIssueService.updateTreeIssueInfo(updateIssueDTO);
        return Result.success();
    }

    @Operation(summary = "查询用户上报异常记录")
    @GetMapping("/records")
    public Result<List<IssueReportsVO>> getUserIssueRecords() {
        log.info("分页查询用户上报异常记录");
        List<IssueReportsVO> issueReportsVOList = treeIssueService.getUserReportIssue();
        return Result.success(issueReportsVOList);
    }
}
